package com.itheima.service.impl;

import com.itheima.dao.VisitorMapper;
import com.itheima.pojo.Visitor;
import com.itheima.service.VisitorService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class VisitorServiceImpl implements VisitorService {
    @Autowired
    private VisitorMapper visitorMapper;

    @Override
    public Map getVisitByWeek(){
        String endDate = DateUtils.dateTimeToStr(new Date());
        String pastDate = DateUtils.getPastDate(6, DateUtils.FORMAT_STRING);
        List<String> dayBetweenDates = DateUtils.getDayBetweenDates(pastDate, endDate);
        List<String> date =new ArrayList<>();
        List<Integer> pv =new ArrayList<>();
        List<Integer> uv =new ArrayList<>();
        for (String dayBetweenDate : dayBetweenDates) {
            Example example =new Example(Visitor.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andLike("createTime",dayBetweenDate+"%");
            int i = visitorMapper.selectCountByExample(example);
            pv.add(i);
            uv.add(visitorMapper.getVisitByWeek(dayBetweenDate).size());
            date.add(DateUtils.getMonthAndDay(dayBetweenDate));
        }
        Map<String,Object>map =new HashMap<>();
        map.put("data",date);
        map.put("uv",uv);
        map.put("pv",pv);
        return map;
    }

    @Override
    public List<Integer> getIpCountByDate(String day) {
        return visitorMapper.getIpCountByDate(day);
    }
}
